package com.hibikiluler.javalearning.mapper;

import com.github.pagehelper.Page;
import com.hibikiluler.javalearning.entity.Comments;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface CommentMapper {
    @Insert("insert into comments(comment, info_id, status, user_id,submit_time) values (#{comment}, #{infoId}, #{status}, #{userId}, #{submitTime})") //增加评论
    void insert(Comments comments);

    @Delete("delete from comments where comment_id=#{id}")  //删除评论
    void delete(Integer id);

    @Select({"<script>",
            "select comments.*,learning_information.title,sys_user.id,sys_user.nick_name from comments",
            "left join sys_user on sys_user.id = comments.user_id",
            "left join learning_information on learning_information.id = comments.info_id",
            "<if test='queryString != null and queryString.length > 0'>",
            "and (title like concat('%', #{queryString}, '%') or content like concat('%', #{queryString}, '%'))",
            "</if>",
            "ORDER BY comments.comment_id",
            "</script>"})
    @Results({
            @Result(column = "comment_id", property = "commentId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "comment", property = "comment"),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "submit_time", property = "submitTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "info_id", property = "infoId", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "nick_name", property = "nickName", jdbcType = JdbcType.BIGINT),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
    })
    Page<Comments> findPage(String queryString); //分页查询评论
}
